package com.bookstore;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.bookstore.bean.Compra;
import com.bookstore.bean.ItemCompra;
import com.bookstore.bean.Libro;
import com.bookstore.db.BookStoreDao;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ViewCartActivity extends MenuListActivity {
	/** Called when the activity is first created. */
	private static final int ACTIVITY_BUY_SUCCESS = 9;

	private BookStoreDao dao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (dao == null) {
			dao = new BookStoreDao(this);
			dao.open();
		}
		setContentView(R.layout.view_cart);
		Button buy = (Button) findViewById(R.id.bn_buy);

		buy.setEnabled(false);
		if (BookStoreActivity.compra.getItems() != null
				&& !BookStoreActivity.compra.getItems().isEmpty()) {
			buy.setEnabled(true);
			loadBooks();
		}

		buy.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {

				Libro book;
				Double total = 0.00;

				for (ItemCompra item : BookStoreActivity.compra.getItems()) {
					book = dao.fetchLibro(item.getIdLibro());
					book.setDisponible(book.getDisponible() - 1);
					dao.updateLibro(book);
					total += book.getPrecio();
				}
				BookStoreActivity.compra.setUsuario(BookStoreActivity.usuario
						.getUsuario());
				BookStoreActivity.compra.setTotal(total);
				Long idCompra = dao.createCompra(BookStoreActivity.compra);

				for (ItemCompra item : BookStoreActivity.compra.getItems()) {
					item.setIdCompra(idCompra);
					dao.createItemCompra(item);
				}

				BookStoreActivity.compra = new Compra();

				Intent i = new Intent(ViewCartActivity.this,
						BuySuccessfulActivity.class);
				startActivityForResult(i, ACTIVITY_BUY_SUCCESS);
			}
		});
	}

	private void loadBooks() {

		String ids;
		List<Long> listIds;
		Cursor c;

		try {
			listIds = new ArrayList<Long>();
			for (ItemCompra item : BookStoreActivity.compra.getItems()) {
				listIds.add(item.getIdLibro());
			}
			ids = listIds.toString();
			ids = ids.replaceAll("\\[", "");
			ids = ids.replaceAll("]", "");
			ids = ids.replaceAll(" ", "");
			
			List<Libro> libros = dao.listLibrosCompra(ids);
			double total = 0.00;

			for (Libro book : libros) {
				total += book.getPrecio();
			}

			if (total > 0) {
				TextView label = (TextView) findViewById(R.id.total);
				label.setText("El total de la compra realizada es: Bs. "
						+ total);
			}
			
			c = dao.fetchLibrosCompra(ids);
			startManagingCursor(c);
			String[] from = new String[] { "title", "autor" };
			int[] to = new int[] { R.id.labelBook, R.id.autorBook };
			SimpleCursorAdapter books = new SimpleCursorAdapter(this,
					R.layout.view_cart_row, c, from, to);
			setListAdapter(books);

			

		} catch (Exception e) {
			Log.e("ViewCartActivity", "Error cargando listView de la compra");
		}
	}
}
